package com.ycw.servlet.guoguo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ycw.dao.LogisticsDao;
import com.ycw.dao.impl.LogisticsDaoImpl;

/**
 * Servlet implementation class AddLogisticsData
 */
@WebServlet("/addLogisticsDataServlet")
public class AddLogisticsDataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * 添加Logistics数据
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/**
		 * 添加工号不能重 这个用来防止数据重复
		 */
		String jobnumber = request.getParameter("l_jobnumber");
		Integer l_jobnumber = Integer.parseInt(jobnumber);
		// 表中的其他基本信息
		String l_name = request.getParameter("l_name");
		String age = request.getParameter("l_age");
		Integer l_age = Integer.parseInt(age);
		String l_gender = request.getParameter("l_gender");
		String l_phone = request.getParameter("l_phone");
		// 身份证
		String l_number = request.getParameter("l_number");
		// 住址
		String l_address = request.getParameter("l_address");
		// list顺序在方法中写死了，尽量不要变
		List<Object> list = new ArrayList<>();
		list.add(l_jobnumber);
		list.add(l_name);
		list.add(l_age);
		list.add(l_gender);
		list.add(l_phone);
		list.add(l_number);
		list.add(l_address);
		LogisticsDao ld = LogisticsDaoImpl.getInstance();
		/**
		 * 添加工号不能重 如果存在返回false ，表示重复 不存在时返回true,表示可以添加
		 */
		boolean notFoundL_id = ld.queryLogisticsL_jobnumber(l_jobnumber);

		boolean addLogisticsData = false;
		if (notFoundL_id) {
			/**
			 * 添加数据 成功为true
			 */
			addLogisticsData = ld.addLogisticsData(list);

		}
		Map<String, Object> map = new HashMap<>();
		/**
		 * notFoundL_id=true表示工号数据不重复 只有notFoundL_id=true时才可以添加
		 * addLogisticsData=true表示添加成功
		 */
		map.put("notFoundL_id", notFoundL_id);
		map.put("addLogisticsData", addLogisticsData);

		String resJSON = JSON.toJSONString(map);
		PrintWriter out = response.getWriter();
		out.write(resJSON);
	}

}
